import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *  检擦对错
 */
public class Judgement extends FileOperation{
    private Integer correct = 0;
    private Integer error = 0;
    private Integer total = 0;
    ArrayList<String> readExerciseList = new ArrayList<String>();
    ArrayList<String> readAnswerList = new ArrayList<String>();
    public void readExerciseFile(String filename){read(readExerciseList,filename);}
    public void readAnswerFile(String filename){read(readAnswerList,filename);}
    public void setJudgeInfo(Integer correct,Integer error){
        this.correct += correct;
        this.error += error;
    }
    public void reset(){
        readAnswerList.clear();
        readExerciseList.clear();
        this.error = 0;
        this.correct = 0;
        this.total = 0;
    }
    public boolean checkSize(){
        return readAnswerList.size() == readExerciseList.size();
    }
    public  Integer  calculate(String formula){
        int opera = formula.contains("+")?1:-1;
        String[] temp= formula.split("[\\+\\-]");
        return Integer.parseInt(temp[0]) + opera*Integer.parseInt(temp[1]);
    }
    public void startJudge(String exerciseFile,String answerFile){
        readExerciseFile(exerciseFile);
        readAnswerFile(answerFile);
        if (readAnswerList.size() == 0 || readExerciseList.size() == 0 ){
            System.out.println("未读取数据或者数据为空！");
        }
        if (!checkSize()) {
            System.out.println("读取的题目和答案长度不一致！");
        }
        File file = new File("result.txt");
        if (file.exists()) {clearFile("result.txt");}
        this.total = readExerciseList.size();
        readExerciseList.forEach(re->{
            if (calculate(re) == Integer.parseInt(readAnswerList.get(readExerciseList.indexOf(re)))){
                setJudgeInfo(1,0);
                save(String.format("%-11s",re +"="+ Integer.parseInt(readAnswerList.get(readExerciseList.indexOf(re)))+"(正确),"),"result.txt");
            }else{
                setJudgeInfo(0,1);
                save(String.format("%-11s",re +"="+ Integer.parseInt(readAnswerList.get(readExerciseList.indexOf(re)))+"(错误),"),"result.txt");
            }
            if ((readExerciseList.indexOf(re)+1) % 5 ==0) {save("\n","result.txt");}
        });
        System.out.println("最终结果");
        System.out.println("题目总数:" + total);
        System.out.println("正确题目数量:" + correct);
        System.out.println("错误题目数量:" + error);
        System.out.println("答案已经保存到result.txt");
        reset();
    }
    public void judgeOnLine(Integer status){
        Exercise exercise = new Exercise();
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入测试的数量：");
        int num = scanner.nextInt();
        this.total = num;
        while (num>0){
            String temp = status == 1?exercise.showOneAddPoeration():
                    status == -1? exercise.showOneSubPoeration():
                            exercise.showOneMixPoeration();
            System.out.print(temp+"=");
            int answer = scanner.nextInt();
            if (answer == calculate(temp)){
                setJudgeInfo(1,0);
            }else{
                setJudgeInfo(0,1);
            }
            num--;
        }
        System.out.println("最终结果");
        System.out.println("题目总数:" + total);
        System.out.println("正确题目数量:" + correct);
        System.out.println("错误题目数量:" + error);
        reset();
    }
}
